/**
 * @author LKQ
 * @date 2022/1/11 12:18
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] prices = {7, 6, 4, 3, 1};
        solution.maxProfit(prices);
    }
    public int maxProfit(int[] prices) {
        int l = 0, r = 1, n = prices.length;
        int maxProfit = 0;
        while (r < n) {
            if (prices[r] <= prices[l]) {
                l = r;
                r++;
            }else {
                while (r < n-1 && prices[r+1] > prices[r]) {
                    r++;
                }
                maxProfit += prices[r] - prices[l];
                l = r + 1;
                r = l + 1;
            }
        }
        return maxProfit;
    }
}
